package mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import modelo.Premio;

public class AdmPersistenciaPremio  extends AdmPersistencia<Premio> {
	
	private static AdmPersistenciaPremio instancia;
	
	private AdmPersistenciaPremio()
	{
		
	}
	
	public static AdmPersistenciaPremio getInstancia()
	{
		if (instancia == null)
			instancia = (AdmPersistenciaPremio) new AdmPersistenciaPremio();
		return instancia;
	}
	
	@Override
	public void insertar(Premio a) {
		// TODO Auto-generated method stub
		try
		{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("insert into dbo.Premio values (?,?,?)");
			//agregar campos
			s.setInt(1, (int) a.getId());
			s.setInt(2, a.getIdTragamonedas());
			s.setFloat(3, a.getValor());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void actualizar(Premio a) {
		// TODO Auto-generated method stub
		try
		{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update dbo.Premio " +
				"set id_premio = ?," +
				"id_tragamonedas =?," +
				"valor =? where id_premio = ?");
			//agregar campos
			s.setLong(1, a.getId());
			s.setInt(2,a.getIdTragamonedas());
			s.setFloat(3, a.getValor());
			s.setLong(1, a.getId());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void borrar(Premio a) {
		// TODO Auto-generated method stub
		try
		{
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("delete from dbo.Premio where id_premio = ?");
			s.setLong(1, a.getId());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

	@Override
	public ArrayList<Premio> seleccionar(long id) {
		// TODO Auto-generated method stub
		ArrayList<Premio> premios;
		
		try
		{
			premios = new ArrayList<Premio>();
			Premio a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select id_premio, id_tragamonedas, valor from dbo.Premio where id_tragamonedas = ?");
			s.setLong(1,id);
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				long id_premio =  result.getLong(1);
				int idTragamonedas =  result.getInt(1);
				float valor = result.getFloat(2);
		
				a = new Premio(id_premio, idTragamonedas, valor);
						
				premios.add(a);
			}
					
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return premios;
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		return null;
	}

	@Override
	public Premio buscar(long id) {
		// TODO Auto-generated method stub
		try
		{
			Premio a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select id_premio, id_tragamonedas, valor from dbo.Premio where id_premio = ?");
			s.setLong(1,id);
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				long id_premio = result.getLong(1);
				int id_tragamonedas = (int) result.getFloat(2);
				float valor = result.getFloat(3);
				
				a = new Premio(id_premio, id_tragamonedas, valor);
			}
			
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
		return null;
	}

}
